package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.FriendListEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.service.IMineFriendService;
import com.forlove.app.qselect.friend.service.impl.MineFriendServiceImpl;
import com.forlove.app.qselect.friend.view.MineFriendView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yjl on 2017/10/23.
 */

public class MineFriendPresenter extends IBasePresenter<IMineFriendService> {
    private IMineFriendService mineFriendService;
    private MineFriendView mineFriendView;

    public MineFriendPresenter(MineFriendView mineFriendView) {
        this.mineFriendView = mineFriendView;
        if (mineFriendService == null) {
            mineFriendService = new MineFriendServiceImpl();
            setService(mineFriendService);
        }

    }

    public void loadFriendListData() {
        mineFriendService.loadData(ServerManager.ACTION_FRIEND_LIST, null, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
//                LogUtil.e("yjl", "好友列表:" + result);
                List<MineFriendEntity> friendEntities = new ArrayList<>();

                friendEntities = ParseHelper.getModelList(result, "data.friends", MineFriendEntity.class);

                FriendListEntity friendListEntity = ParseHelper.getModel(result, "data", FriendListEntity.class);

                if (friendListEntity != null) {
                    FriendManager.saveFriendList(ForloveApplication.getInstance(), result);
                }
                if (mineFriendView != null) {
                    mineFriendView.updateFriendListView(friendEntities);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (mineFriendView != null) {
                        mineFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (mineFriendView != null) {
                        mineFriendView
                                .showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

}
